<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增请假记录')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-leave-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">人员类型：</label>
                <div class="col-sm-8">
                    <select name="userType" id="userType" class="form-control m-b" th:onchange="changeType()">
                        <option value="01">普通职工</option>
                        <option value="02">中层及以上</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">代理人：</label>
                <div class="col-sm-8">
                    <input id="agentUser" name="agentUser" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">休假类型：</label>
                <div class="col-sm-8">
                    <select name="leaveType" class="form-control m-b" th:with="type=${@dict.getType('record_leave_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">请假事由：</label>
                <div class="col-sm-8">
                    <textarea name="leaveContent" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime" id="startTime" class="form-control" placeholder="yyyy-MM-dd" type="text" th:onchange="changeDays()" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" id="endTime" class="form-control" placeholder="yyyy-MM-dd" type="text" th:onchange="changeDays()" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">天数：</label>
                <div class="col-sm-8">
                    <input name="days" id="days" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="remark" class="form-control"></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "record/leave"
        $("#form-leave-add").validate({
            focusCleanup: true
        });

        changeType();

        function changeType() {
            var userType = $("#userType").val();
            if(userType == '01'){
                $("#agentUser").parents(".form-group").hide();
            }else{
                $("#agentUser").parents(".form-group").show();
            }
        }

        function changeDays() {
            var startTime = $("#startTime").val();
            var endTime = $("#endTime").val();
            if(startTime != '' && endTime != ''){
                var days = (Date.parse(endTime) - Date.parse(startTime)) / (24 * 60 * 60 * 1000);
                $("#days").val(days + 1);
            }
        }

        function submitHandler() {
            var userType = $("#userType").val();
            var agendId = $("#treeId").val();
            if(userType == '02' && agendId == ''){
                $.modal.alertError("请选择代理人");
                return;
            }
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-leave-add').serialize());
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            max: $('#endTime').val(),
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            min: $('#startTime').val(),
            autoclose: true
        });
    </script>
</body>
</html>